<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="用户" prop="userId">
        <el-select
          v-model="queryParams.userId"
          placeholder="用户"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in userList"
            :key="dict.userId"
            :label="dict.nickName"
            :value="dict.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="试卷" prop="paperId">
        <el-select
          v-model="queryParams.paperId"
          placeholder="试卷"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in paperList"
            :key="dict.paperId"
            :label="dict.name"
            :value="dict.paperId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="结果" prop="answerResult">
        <el-select
          v-model="queryParams.answerResult"
          placeholder="学科"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.answer_result"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <span v-show="totalScore>0">得分：{{ totalScore }}</span>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="scoreList">
      <el-table-column label="用户" align="center" prop="userId" width="100">
        <template slot-scope="scope">
          {{ getUserData(scope.row.userId) }}
        </template>
      </el-table-column>
      <el-table-column label="试卷" align="center" prop="paperId" width="300">
        <template slot-scope="scope">
          {{ getPaperData(scope.row.paperId) }}
        </template>
      </el-table-column>
      <el-table-column label="回答结果" align="center" prop="answerResult" width="200">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.answer_result" :value="scope.row.answerResult"/>
        </template>
      </el-table-column>
      <el-table-column label="得分" align="center" prop="score" width="100"/>
      <el-table-column label="题目" align="left" prop="topicId">
        <template slot-scope="scope">
          {{ getTopicData(scope.row.topicId) }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listScore} from "@/api/base/score";
import {listPaper} from "@/api/base/paper";
import {listUser} from "@/api/system/user";
import {listTopic} from "@/api/base/topic";

export default {
  name: "Score",
  dicts: ['answer_result', 'question_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 得分表格数据
      scoreList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        paperId: null
      },
      totalScore: 0,
      userList: [],
      paperList: [],
      topicList: []
    };
  },
  created() {
    this.getUserList().then(v1 => {
      this.getPaperList().then(v2 => {
        this.getTopicList().then(v3 => {
          this.getList();
        });
      });
    });
  },
  methods: {
    getUserList() {
      return Promise.resolve(
        listUser({pageNum: 1, pageSize: 10000000}).then(response => {
          this.userList = response.rows.filter(d => d.deptId == 105);
        })
      );
    },
    getPaperList() {
      return Promise.resolve(
        listPaper({pageNum: 1, pageSize: 10000000}).then(response => {
          this.paperList = response.rows;
        })
      );
    },
    getTopicList() {
      return Promise.resolve(
        listTopic({pageNum: 1, pageSize: 10000000}).then(response => {
          this.topicList = response.rows;
        })
      );
    },
    /** 查询得分列表 */
    getList() {
      this.loading = true;
      const p = this.paperList.filter(d => d.paperId == this.queryParams.paperId)[0];
      if (this.queryParams.userId && p) {
        this.queryParams.pageSize = p.mustFillNum + p.mustChoiceNum + p.mustSayNum + p.mustExplainNum + p.lootFillNum + p.lootChoiceNum + p.lootSayNum + p.lootExplainNum;
      }
      this.totalScore = 0;
      listScore(this.queryParams).then(response => {
        this.scoreList = response.rows;
        this.total = response.total;
        if (this.queryParams.userId && p) {
          this.scoreList.forEach(d => {
            this.totalScore += d.score;
          });
        }
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getUserData(userId) {
      if (this.userList.length > 0) {
        return this.userList.filter(d => d.userId == userId)[0].nickName;
      }
    },
    getPaperData(paperId) {
      if (this.paperList.length > 0) {
        return this.paperList.filter(d => d.paperId == paperId)[0].name;
      }
    },
    getTopicData(topicId) {
      if (this.topicList.length > 0) {
        const topic = this.topicList.filter(d => d.topicId == topicId)[0];
        return topic.number + "、【" + this.dict.type.question_type[topic.questionType-1].label + "】" + topic.name;
      }
    }
  }
}
;
</script>
